package von.seiji.cn.test;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamUtils;
import com.github.sarxos.webcam.util.ImageUtils;
import com.google.common.collect.Lists;
import org.junit.Test;
import von.seiji.cn.SelfUtil;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class TT {

    /**
     * 拍照
     *
     * @param args
     */
    public static void main(String[] args) {

        Webcam webcam = Webcam.getDefault();
        Timer timer = new Timer("L", false);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.err.println(Thread.currentThread().getName() + " 尝试捕获图片！！！");
                String fileName = "D://LL/" + System.currentTimeMillis();       //保存路径即图片名称（不用加后缀）
                WebcamUtils.capture(webcam, fileName, ImageUtils.FORMAT_PNG);
            }
        }, 0, 10);
    }

    @Test
    public void test1() {
        Object a = "0";
        System.out.println("0".equals(a));
    }

    @Test
    public void tes5() {
        String str = "C:\\Users\\Administrator\\Pictures\\20211102153840.jpg";
        String s = SelfUtil.drawAscii(" . !+%$#@", new File(str));
        FileUtil.writeString(s, new File("C:\\Users\\Administrator\\Pictures\\11.txt"), "utf-8");
    }

    @Test
    public void tesatasdf() {
        List<String> analyzer = SelfUtil.analyzer("今天早上起床晚了，没赶上公交车，所以走路去公司上班！！", true);
        System.out.println("analyzer = " + analyzer);
    }

    @Test
    public void tlakjfdlsJ() {
        String a = "ffff:183.193.173.87,127.0.0.1";
        String reg = "((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(1\\d\\d|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)";
        System.out.println(Pattern.matches(reg, a));
        Matcher matcher = Pattern.compile(reg).matcher(a);
        ArrayList<String> objects = Lists.newArrayList();
        while (matcher.find()) {
            System.out.println(matcher.group());
            objects.add(matcher.group());
        }
        System.out.println("objects = " + objects);

        String s = objects.stream().filter(item -> item.length() > 1).findAny().orElse("111111");
        System.out.println("s = " + s);

    }

    @Test
    public void lsajfd() {
        /*
         * Path normalize()
         * 移除如 . 或者 .. 等冗余的路径元素
         */
        Path p6 = Paths.get("/ttt",".","test",".","test.txt");
        System.out.println("p6 = " + p6);
        Path p7 = p6.normalize();
        System.out.println("normalize: " + p7);
        System.out.println(p6.getRoot());
        System.out.println("p6.toAbsolutePath() = " + p6.toAbsolutePath());
    }

    @Test
    public void dafd() {
        Path absolutePath = Paths.get("/test1", "test2");
        Path relativePath = Paths.get("root", "test1", "test2", "test3", "test.txt");
        /*
         * Path relativize(Path other)
         * 返回用 this 进行解析，相对于 other 的相对路径
         */
        Path relativePath2 = Paths.get("root", "test1", "test2", "test4", "test2.txt");
        Path p5 = relativePath.relativize(relativePath2);
            System.out.println("relativize: "+p5);
    }

    @Test
    public void flsakd() {
        ArrayList<String> list = Lists.newArrayList("111","aaa", "cc", "lksfi", "aaa", "888");
        System.out.println("list = " + list);

        List<String> collect = list.stream().sorted(Comparator.naturalOrder()).collect(Collectors.toList());
        System.out.println("collect = " + collect);
        List<String> collect1 = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        System.out.println("collect1 = " + collect1);
        //根据索引映射出逆序的索引值（取相反数）比较，使list集合反转，但是，明显的缺陷是，集合中不能出现两个相同的元素，否则indexOf获取的索引不正确
        List<String> collect2 = list.stream().sorted(Comparator.comparingInt(item -> -list.indexOf(item))).collect(Collectors.toList());
        System.out.println("collect2 = " + collect2);
        List<String> collect3 = IntStream.range(0, list.size()).mapToObj(item -> list.get(list.size() - 1 - item)).collect(Collectors.toList());
        System.out.println("collect3 = " + collect3);
        ArrayList<String> clone = (ArrayList<String>) list.clone();
        Collections.reverse(clone);
        System.out.println("clone = " + clone);


    }


}
